package com.company.project.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;

public class DateUtil
{

	public static void main(String[] args)
	{
		LoggerUtils.error("afaf");
	}

	public static String yyyyMMdd(Date date)
	{
		try
		{
			SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd");
			String retStrFormatNowDate = sdFormatter.format(date);
			return retStrFormatNowDate;

		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return "";
	}

	public static Date yyyyMMdd(String time)
	{
		try
		{
			SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd");
			Date date = sdFormatter.parse(time);
			return date;

		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}

	public static String MMdd(Date date)
	{
		try
		{
			SimpleDateFormat sdFormatter = new SimpleDateFormat("MM-dd");
			String retStrFormatNowDate = sdFormatter.format(date);
			return retStrFormatNowDate;

		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return "";
	}

	public static String HHmm(Date date)
	{
		try
		{
			SimpleDateFormat sdFormatter = new SimpleDateFormat("HH:mm");
			String retStrFormatNowDate = sdFormatter.format(date);
			return retStrFormatNowDate;

		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return "";
	}

	public static String yyyyMMddHHmm(Date date)
	{
		try
		{
			SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String retStrFormatNowDate = sdFormatter.format(date);
			return retStrFormatNowDate;

		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return "";
	}

	public static Date yyyyMMddHHmm(String time)
	{
		try
		{
			SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date date = sdFormatter.parse(time);
			return date;

		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}

	public static String yyyyMMddHHmmss(Date date)
	{

		try
		{
			SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String retStrFormatNowDate = sdFormatter.format(date);
			return retStrFormatNowDate;

		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return "";
	}

	// -----------------------------------------------------------------------
	/**
	 * 获取当前日期加上天数后的日期 Adds a number of days to a date returning a new object.
	 * The original date object is unchanged.
	 *
	 * @param date
	 *            the date, not null
	 * @param amount
	 *            the amount to add, may be negative
	 * @return the new date object with the amount added
	 * @throws IllegalArgumentException
	 *             if the date is null
	 */
	public static Date addDays(Date date, int amount)
	{
		return DateUtils.addDays(date, amount);
	}

	/**
	 * 得到n天之后是周几
	 *
	 * @param days
	 * @return
	 */
	public static String getAfterDayWeek(String days)
	{
		int daysInt = Integer.parseInt(days);

		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
		Date date = canlendar.getTime();

		SimpleDateFormat sdf = new SimpleDateFormat("E");
		String dateStr = sdf.format(date);

		return dateStr;
	}

	/**
	 * 获取当前时间到(凌晨+addHours)的时间距离
	 * 
	 * @param addHours
	 * @return milliseconds
	 */
	public static long getTimeDistanceFromBeforeDawn(int addHours)
	{
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + 1);
		cal.set(Calendar.HOUR_OF_DAY, addHours);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		long diff = cal.getTimeInMillis() - System.currentTimeMillis();
		return diff;
	}

	/**
	 * 获取前后日期 , days为正数 向后推迟days天，负数时向前提前days天
	 * 
	 * @param days
	 * @return date
	 */
	public static Date getForwardBackwardDate(int days)
	{
		Date dat = null;
		Calendar cd = Calendar.getInstance();
		cd.add(Calendar.DATE, days);
		dat = cd.getTime();
		return dat;
	}

}
